<template>
  <div class="password">
    <el-card class="box-card">
      <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          style="width: 400px"
      >
        <el-form-item label="原密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
          >
          <el-button @click="$refs['ruleForm'].resetFields()">重置</el-button>
        </el-form-item>
      </el-form>

</el-card>
  </div>
</template>
<script>
export default {
  data() {
    const validateOldPass = (rule, value, callback) => {
      if (value.toString().length < 6) {
        callback(new Error("密码必须大于6位数"));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value != this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userInfo: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      ruleForm: {
        oldPass: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        oldPass: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" },
        ],
        pass: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" },
        ],
        checkPass: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validatePass, trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let pwdDto = {
            username: this.userInfo.data.username,
            password: this.ruleForm.oldPass,
            newPassword: this.ruleForm.checkPass,
          }
          this.request.post("/user/pwd", pwdDto).then((res) => {
            if (res.data.code === "200") {
              this.$message.success("修改成功!请重新登录。");
              localStorage.removeItem("user");
              this.$emit("call-father");
              this.$router.push("/login");
            } else {
              this.$message.error(res.date.msg);
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
 .password {
    margin-top: 50px;
    margin-left: 450px;
    .el-form-item {
      width: 80%;
    }
    .el-card{
        width: 70%;
    }
}
</style>